package com.situ.lession1226.controller;

import com.situ.lession1226.model.User;
import com.situ.lession1226.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private UserService userService;

    /*@Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }*/

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 处理/list请求，列出学生信息列表
     *
     * @return
     */
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE})
    @GetMapping("/list")
    public String list(Map<String, Object> map) {
        //List<User> list = userService.findAll();

        //map.put("users", list);

        return "user/list";
    }

    /**
     * 处理/user/add请求
     *
     * @return
     */
    @GetMapping("/add")
    public String add(Map<String, Object> map) {
        map.put("showSubmit", false);//不显示提交按钮
        return "user/add";
    }

    /**
     * 进入修改页面
     *
     * @param map
     * @return
     */
    @GetMapping("/edit")
    public String edit(Integer id, Map<String, Object> map) {
        User user = userService.findById(id);
        if (user == null) {
            map.put("error", "要修改的用户不存在");
        } else {
            map.put("stu", user);
        }
        return "user/edit";
    }
}
